<?php

namespace App\Models\Member;

use App\Models\BaseModel;

class PostageEms extends BaseModel
{
    protected $fillable = [
        'id', 'code', 'title', 'every', 'ykg',
        'first', 'second',
        'created_at', 'updated_at'
    ];
    // 插入邮费信息
    public function insertEms($storeId)
    {
        if (empty($storeId)) {
            return $this->errorClient($storeId, '请传递正确的店铺ID');
        }
        // 检查店铺邮费信息是否插入, 如果已经插入 , 则返回 邮费信息已存在
        $data = $this->where('store_id', $storeId)->get()->toArray();
        if (!empty($data)) {
            return $this->backJsonClient($storeId, '邮费数据已经存在');
        }
        // 获取原始0ID数据
        $dataIni = $this->where('store_id', 0)
        ->select('store_id', 'code', 'title', 'every', 'ykg', 'first', 'second')
        ->get()->toArray();
        // 使店铺ID 为指定数据 , 插入
        $dataUp = $this->foreachEms($dataIni, $storeId);
        $insert = $this->insert($dataUp);
        if (empty($insert)) {
            return $this->errorClient($storeId, '邮费信息插入失败!');
        } else {
            return $this->successClient($storeId, '邮费信息插入成功!'); 
        }
    }
    //
    public function foreachEms($dataIni, $storeId)
    {
        foreach ($dataIni as $key => $val) {
            $dataIni[$key]['store_id'] = $storeId;
            $dataIni[$key]['created_at'] = date("Y-m-d H:i:s");
        }
        return $dataIni;
    }
    public function expEms($nationalCode, $weightArr, $storeId = 0)
    {
        $exp = 0;//邮费
        $nationalCode = substr($nationalCode , 0 , 2).'0000';
        $rule = $this->where('store_id', $storeId)->where('code', $nationalCode)->first();
        if (empty($rule)) {
            return $this->errorClient($storeId, '邮费信息不存在!');
        }
        
        $firstOne = $rule['every'];// 首重 500
        $firstTwo = 1000;//首重 1000
        $sec = $rule['every'];// 续重 500

        $firstPriceOne=$rule['ykg'];//500首重费用
        $firstPriceTwo = $rule['first'];// 1000首重费用
        $secPrice = $rule['second'];// 续重费用
        
        // 包邮 - 不包邮 且 不分担 / 不包邮 且 分单 / 不包邮 且 单独算一单 / 不包邮 且 单独算一单的余单
        if ($weightArr['exp'] != 0) {
            $exp = $this->countEms($weightArr['exp'], $exp, $firstOne, $firstTwo, $sec, $firstPriceOne, $firstPriceTwo, $secPrice);
        }
        if (!empty($weightArr['expFen'])) {
            foreach ($weightArr['expFen'] as $keyFen => $valueFen) {
                $fenWeight = $valueFen['weight']*$valueFen['listNum'];
                $exp = $this->countEms($fenWeight, $exp, $firstOne, $firstTwo, $sec, $firstPriceOne, $firstPriceTwo, $secPrice);
            }
        }
        if (!empty($weightArr['expSole'])) {
            foreach ($weightArr['expSole'] as $keySole => $valueSole) {
                $soleWeight = $valueSole['weight']*$valueSole['listNum'];
                $exp = $this->countEms($soleWeight, $exp, $firstOne, $firstTwo, $sec, $firstPriceOne, $firstPriceTwo, $secPrice);
            }
        }
        if (!empty($weightArr['expSoleYu'])) {
            foreach ($weightArr['expSoleYu'] as $keySoleYu => $valueSoleYu) {
                $soleWeight = $valueSoleYu['weight']*$valueSoleYu['listNum'];
                $exp = $this->countEms($soleWeight, $exp, $firstOne, $firstTwo, $sec, $firstPriceOne, $firstPriceTwo, $secPrice);
            }
        }
        return $this->successClient($exp, '邮费信息插入成功!'); 
    }
    // 重量 / 初始邮费 / 500首重 / 1000首重 / 续重 / 500首重费用 / 1000首重费用 / 续重费用 => 返回 最终邮费
    public function countEms($weight, $exp, $firstOne, $firstTwo, $sec, $firstPriceOne, $firstPriceTwo, $secPrice)
    {
        if ($weight == 0) {
            $exp += 0;
        } elseif ($weight > 0 && $weight <= $firstOne) {
            $exp += $firstPriceOne;// 500首重
        }elseif ($weight > $firstOne && $weight <= $firstTwo) {
            $exp += $firstPrice;
        } else {
            $cha = $weight - $firstTwo;
            $secNum = ceil($cha / $sec);
            $exp += $firstPriceTwo + $secNum * $secPrice;
        }
        return $exp;
    }
}
